keep = ls()

###########
#Load Data#
###########

legislators = fread('./data/iowa_legislators/cleaned/ia_legislators_use.csv')


##########
#Table C2#
##########

g1 = glm(full_suff ~ vet_pct , legislators[(republican)], family = binomial)
g2 = glm(full_suff ~ vet_pct + yes_57 + r_57 + chg , legislators[(republican)], family = binomial)
g3 = glm(full_equal ~ vet_pct , legislators[(republican)], family = binomial)
g4 = glm(full_equal ~ vet_pct + yes_57 + r_57 , legislators[(republican)], family = binomial)


ia_legis = stargazer(list(g1,g2,g3,g4), type = 'latex',
                     keep = c('vet_pct', 'yes_57', 'r_57'),
                     keep.stat = 'n',
                     dep.var.labels  = c("Universal Suffrage", "Full Equality"),
                     covariate.labels = c("Enlistment Rate", "Pro-Suffrage VS (1857)", "Republican VS (1857)"),
                     title = "Predictors of Iowa Republican Support for Removing Racial Qualifications",
                     label = "tab:ia_legislators"
)

note = "Estimates from logit regression. Observations are Iowa General Assembly constituencies held by Republicans in 1866. ``Universal Suffrage'' indicates a vote against amending the proposed constitutional amendment to grant limited suffrage to African Americans and then voting for suffrage extension. ``Full Equality'' further indicates voting to eliminate all racial qualifications in the Constitution. Enlistment rate is number of men serving over those of military age in 1860. Suffrage voteshares come from state constitutional referenda returns in 1857.  Republican voteshares come from the gubernatorial election of 1857."
write_latex(ia_legis, note, './output/appendix/Table_C2.tex')


###########
#Figure C6#
###########

int = inter.binning(legislators,
                    Y = 'full_suff',
                    D = "republican",
                    X = "vet_pct",
                    Z = c("yes_57", "r_57"),
                    na.rm = T,
                    vartype = 'bootstrap',
                    nboots = 1000,
                    full.moderate = T,
                    theme.bw = T,
                    nbins = 3,
                    Ylabel = "Full Suffrage",
                    Xlabel = 'Enlistment Rate',
                    Dlabel = "Republican"
)

ggsave("./output/appendix/Figure_C6.pdf", int$graph, width = 6, height = 5)

##############################################################
#Cleanup
rm(list = setdiff(ls(), c(keep, 'keep')))
gc()